package io.github.talelin.latticy.controller.v1;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.github.talelin.core.annotation.GroupRequired;
import io.github.talelin.core.annotation.PermissionMeta;;
import io.github.talelin.latticy.bo.InboundOrderDetailsVo;
import io.github.talelin.latticy.bo.InboundOrderPageVo;
import io.github.talelin.latticy.bo.InboundOrderSkuVo;
import io.github.talelin.latticy.common.util.PageUtil;
import io.github.talelin.latticy.dto.feign.InboundOrderPageDTO;
import io.github.talelin.latticy.dto.receving.ReceivingRecordDTO;
import io.github.talelin.latticy.dto.receving.ReceivingRecordSkuDTO;
import io.github.talelin.latticy.service.ReceivingRecordService;
import io.github.talelin.latticy.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.Positive;


/**
 * 入库收货记录表前端控制器
 *
 * @author generator@wulongbo
 * @since 2022-06-29
 */
@RestController
@RequestMapping("/v1/receivingRecord")
public class ReceivingRecordController {

    @Autowired
    private ReceivingRecordService receivingRecordService;

    @PostMapping("/createReceivingRecord")
    @GroupRequired
    @PermissionMeta(value = "新增收货记录", mount = false)
    public CreatedVO create(@RequestBody ReceivingRecordDTO recordCreate) {
        receivingRecordService.createReceivingRecord(recordCreate);
        return new CreatedVO();
    }

    @PostMapping("/createReceivingSkuRecord")
    @GroupRequired
    @PermissionMeta(value = "新增sku收货记录", mount = false)
    public CreatedVO createReceivingSkuRecord(@RequestBody ReceivingRecordSkuDTO recordCreate) {
        receivingRecordService.createReceivingSkuRecord(recordCreate);
        return new CreatedVO();
    }

    @DeleteMapping("/redo/{ids}")
    @GroupRequired
    @PermissionMeta(value = "重做", mount = false)
    public DeletedVO delete(@PathVariable Long[] ids) {
        receivingRecordService.redoReceivingRecord(ids);
        return new DeletedVO();
    }

    @GetMapping("/{inboundOrderNo}")
    @GroupRequired
    @PermissionMeta(value = "根据入库单查询收货记录列表", mount = false)
    public PageResponseVO<ReceivingRecordVO> get(@PathVariable(value = "inboundOrderNo") @Positive(message = "{inbound-order.id.not-empty}") String inboundOrderNo,
                                                 @RequestParam(name = "page", required = false, defaultValue = "0")
                                                 @Min(value = 0, message = "{page.number.min}") Integer page,
                                                 @RequestParam(name = "count", required = false, defaultValue = "10")
                                                 @Min(value = 1, message = "{page.count.min}")
                                                 @Max(value = 30, message = "{page.count.max}") Integer count) {
        IPage<ReceivingRecordVO> iPage = receivingRecordService.getReceivingRecordPage(inboundOrderNo, count, page);
        return PageUtil.build(iPage);
    }

    @GetMapping("/getBySkuReceivingRecord/{inboundOrderNo}")
    @GroupRequired
    @PermissionMeta(value = "根据入库单查询收货记录详情", mount = false)
    public ReceivingRecordBySkuVO getBySkuReceivingRecord(@PathVariable(value = "inboundOrderNo") @Positive(message = "{inbound-order.id.not-empty}") String inboundOrderNo) {
        return receivingRecordService.getBySkuReceivingRecord(inboundOrderNo);
    }

    @GetMapping("/page")
    @GroupRequired
    @PermissionMeta(value = "入库单列表", mount = false)
    public PageResponseVO<InboundOrderPageVo> page(@Validated InboundOrderPageDTO dto) {
        return receivingRecordService.getInboundOrder(dto);
    }





    @GetMapping("/receiving/{inboundOrderNo}")
    @GroupRequired
    @PermissionMeta(value = "根据入库单号查询入库单", mount = false)
    public InboundOrderDetailsVo getInboundOrderByNo(@PathVariable(value = "inboundOrderNo") String inboundOrderNo) {
        return receivingRecordService.getInboundOrderByNo(inboundOrderNo);
    }

    @GetMapping("/inboundOrderSku/{inboundOrderNo}")
    @GroupRequired
    @PermissionMeta(value = "根据入库单号查询入库单", mount = false)
    public InboundOrderSkuVo getInboundOrderSkuByNo(@PathVariable(value = "inboundOrderNo") String inboundOrderNo) {
        return receivingRecordService.getInboundOrderSkuByNo(inboundOrderNo);
    }
}
